﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using University.Workshop.Model;
using University.Workshop.DataAccess;
using University.Workshop.Business;
using System.Web.Security;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.ObjectModel;
using System.Text;

namespace University.Workshop.WebApp
{
    public partial class ProductUnsubscribe : System.Web.UI.Page
    {
       Collection<Product> products;
        protected void Page_Load(object sender, EventArgs e)
        {            
            if (!User.Identity.IsAuthenticated)
            {
                FormsAuthentication.RedirectToLoginPage();
            }

            if (!IsPostBack)
            {
                products = new Collection<Product>();

                try
                {
                    products = ProductDataAccess.GetProductsBySeller(UserDataAccess.GetUserByEmail(User.Identity.Name).Id, true);
                    GridView1.DataSource = products;

                    if (products.Count == 0)
                    {
                        errorMsg.Visible = true;
                        errorMsg.Text = "You don't have products on Sale";
                    }
                    GridView1.DataBind();
                }
                catch (Exception er)
                {
                    errorMsg.Visible = true;
                    errorMsg.Text = er.Message;
                }
            }
        }



        protected void UnsubscribeFromSale_Click(object sender, EventArgs e)
        {
            try
            {
                foreach (GridViewRow row in GridView1.Rows)
                {
                    HtmlInputCheckBox aux = ((HtmlInputCheckBox)row.FindControl("chkSelect"));
                    //CheckBox aux = ((CheckBox)row.FindControl("chkSelect"));
                    bool isChecked = aux.Checked;

                    if (isChecked)
                    {
                        string pid = ((Label)row.Cells[0].FindControl("ProductID")).Text;
                        long id = long.Parse(pid);
                        ProductUnsubscribeManager.UnsubscribeProduct(id);
                    }
                }
                Response.Redirect("ProductUnsubscribe.aspx");
            }
            catch (Exception er)
            {
                errorMsg.Visible = true;
                errorMsg.Text = er.Message;
            }
            
        }

           
    }
}